package main

import (
	"common/config"
	myconn "common/conn"
	"common/helper"
	myredis "common/redis"
	"common/userHelp"
	"context"
	"github.com/BurntSushi/toml"
	"github.com/meilisearch/meilisearch-go"
	"github.com/valyala/fasthttp"
	_ "go.uber.org/automaxprocs"
	"log"
	"member/contrib/conn"
	"member/middleware"
	"member/model"
	"member/pkg"
	"member/router"
	"os"
)

func main() {

	helper.InitLog()
	passphrase := "myverystrongpasswordo32bitlength" // 32字节的密钥
	configFile := "br1.enc"                          // 加密后的配置文件路径

	helper.InfoLog("step 111")
	// 读取并解密配置文件
	decryptedData, err := helper.LoadEncryptedConfig(configFile, passphrase)
	if err != nil {
		helper.InfoLog("step 2222")
		log.Fatalf("Error reading encrypted config file: %v", err)
	}
	cfg := pkg.Conf{}
	//endpoints := strings.Split(os.Args[1], ",")
	//apollo.New(endpoints, pkg.ETCDName, pkg.ETCDPass)
	//err := apollo.ParseTomlStruct(os.Args[2], &cfg)
	//_, err := toml.DecodeFile(os.Args[1], &cfg)
	if err := toml.Unmarshal(decryptedData, &cfg); err != nil {
		log.Fatalf("Error parsing config file: %v", err)
	}

	mt := new(model.MetaTable)

	helper.InfoLog("step 333")
	mt.Program = cfg.Prefix
	mt.Email = cfg.Email
	mt.AwsEmail = cfg.AwsEmail
	mt.IpDB = conn.InitIpDB(cfg.Ipdb)
	mt.MerchantDB = conn.InitDB(cfg.Db.Member, cfg.Db.MaxIdleConn, cfg.Db.MaxOpenConn)
	mt.SlaveDB = conn.InitDB(cfg.SlaveDb.Member, cfg.SlaveDb.MaxIdleConn, cfg.SlaveDb.MaxOpenConn)
	//mt.MerchantBean = conn.BeanNew(cfg.Beanstalkd)
	mt.MerchantRedis = conn.InitRedis(cfg.Redis.Addr[0], cfg.Redis.Password, cfg.Redis.Db)
	mt.RedisDBIndex = cfg.Redis.Db
	//mt.MerchantRedis = conn.InitRedisSentinel(cfg.Redis.Addr, cfg.Redis.Password, cfg.Redis.Sentinel, 0)

	helper.InfoLog("step 4444")
	mt.Meili = meilisearch.NewClient(meilisearch.ClientConfig{
		Host:   cfg.Meilisearch.Host,
		APIKey: cfg.Meilisearch.Key,
	})

	helper.InfoLog("step 55555")
	mt.TgPay.AppKey = cfg.Tgpay.AppKey

	myconn.New("0.0.0.0")

	model.Constructor(mt, cfg.RPC)

	defer func() {
		model.Close()
		mt = nil
	}()

	b := router.BuildInfo{
		GitReversion:   pkg.GitReversion,
		BuildTime:      pkg.BuildTime,
		BuildGoVersion: pkg.BuildGoVersion,
	}
	mt.Contate = cfg.Contate
	mt.WebUrl = cfg.WebURL
	mt.Callback = cfg.Callback
	//app := router.SetupRouter(b)
	appNew := router.SetupNewRouter(b)
	config.InitCfg(mt.MerchantDB, mt.MerchantRedis)
	myredis.Init(context.Background(), mt.MerchantRedis, mt.MerchantDB, mt.SlaveDB, userHelp.LoadUserToRedis)
	userHelp.LoadModuleInit(mt.MerchantDB, myredis.SetUserFieldNotLab)

	resGameType := config.CopyGameType()
	helper.InfoLog("resGameType:%v", resGameType)
	go model.UpdateUserDataToSql(context.Background(), mt.MerchantRedis, mt.MerchantDB)
	go model.UpdateLogToSql(mt.MerchantRedis, mt.MerchantDB)
	go model.StartTimer()
	go userHelp.TimerAddFakeDeposit()

	userHelp.InitLottery()
	//userHelp.AutoLottery()

	srv := &fasthttp.Server{
		Handler:            middleware.Use(appNew.Handler),
		ReadTimeout:        router.ApiTimeout,
		WriteTimeout:       router.ApiTimeout,
		Name:               "member",
		MaxRequestBodySize: 51 * 1024 * 1024,
	}

	//helper.InfoLog("member service start")
	//userHelp.SwitchParent("93008098", "50869333")

	//proxyCtl := new(controller.ProxyController)
	//proxyCtl.QueryProxyDailyRunning(nil)
	//proxyCtl.QueryProxyDailyCommi(nil)

	//proxyCtl.QueryChildGameRecord(nil)
	//proxyCtl.QueryProxyDailyRunning(nil)
	//proxyCtl.QueryProxyDailySubDetail(nil)

	//userHelp.LoadUserToRedis("44943772")
	//turnTableCtl := new(controller.TurnTableController)
	//turnTableCtl.TurnTableOtherRecord(nil)
	//turnTableCtl.TurnTableCfg(nil)
	if err := srv.ListenAndServe(cfg.Port.Member); err != nil {
		helper.InfoLog("Error in ListenAndServe: %s", err.Error())
		os.Exit(1)
	}
}
